/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.controllers;

import aso.upna.playlist.GenericIterator;
import aso.upna.playlist.GenericList;
import aso.upna.threads.ServerListenerThread;
import java.awt.Color;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author triki
 */

public class UploadTableController {

    private JTable ref_table;
    private ServerListenerThread ref_server;
    private GenericList ref_list;
    private final int DEF_ROWS = 4;
    
    public UploadTableController(JTable table)
    {
        ref_table = table;
    }

    public void registerServer(ServerListenerThread server)
    {
        ref_server = server;
    }

    public void registerList(GenericList list)
    {
        ref_list = list;
    }
    
    public void refresh()
    {
        int i,rows;
        GenericIterator iter = null;
        DefaultTableModel tab;
        Object obj[] = null;
        SendInfo info = null;
        JProgressBar jp = null;
        TableColumn t_col = null;

        cleanTable();

        if(ref_list.getCountElements() <= 0)
            return;

        tab = (DefaultTableModel) ref_table.getModel();

        rows = tab.getRowCount();
        i = 0;
        
        iter = ref_list.getIterator();

        ProgressBarRenderer jpb_render = new ProgressBarRenderer();
        ColoredTableCellRenderer colren = new ColoredTableCellRenderer();

        //ref_table.getColumnModel().getColumn(3).setCellRenderer(colren);
        //colren.setValue(Color.RED);
        while(iter.hasNext())
        {
            if(i > rows-1)
            {
                tab.addRow(obj);
            }
          
            Component cp = jpb_render.getTableCellRendererComponent(ref_table,null,false,false,i, 3);
            jp = (JProgressBar) cp;
            
            info = (SendInfo) iter.current();

            jp = new JProgressBar(0,100);
            jp.setValue((int) info.porcentaje());

            ref_table.setValueAt(info.getUserId(),i,0);
            ref_table.setValueAt(info.getIp(), i, 1);
            ref_table.setValueAt(info.getCancion(), i, 2);
            jp.setValue((int)info.porcentaje());
            ref_table.setValueAt((int)info.porcentaje()+"% completado", i, 3);
            iter.next();
            i++;
        }
        
        /*SendInfo info = (SendInfo) ref_list.getIterator().current();
        System.out.println("uid: "+info.getUserId()+" progreso: " + info.porcentaje());*/

    }

    private void cleanTable()
    {
        int rows,cols,i,j;

        DefaultTableModel tab = (DefaultTableModel) ref_table.getModel();
        rows = ref_table.getRowCount();
        cols = ref_table.getColumnCount();

        tab.setNumRows(DEF_ROWS);

        rows = ref_table.getRowCount();

        for(i=0;i<rows;i++)
        {
            for(j=0;j<cols;j++)
            {
                ref_table.setValueAt(null, i, j);
            }
        }
    }
}
